package com.wsj.a.collapsingdemo.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.wsj.a.collapsingdemo.callback.LoginCallback;
import com.wsj.a.collapsingdemo.entity.QQUserInfo;
import com.wsj.a.collapsingdemo.entity.WBUserInfo;
import com.wsj.a.collapsingdemo.entity.WXUserInfo;
import com.wsj.a.collapsingdemo.utils.Logger;
import com.wsj.a.collapsingdemo.utils.ToastUtil;

import java.util.Map;

/**
 * Created by linxiujie on 2018/8/16.
 */

public class ThirdLoginPresenter {

    private Context mContext;
    //登录到凉瓜应用时回调
    private LoginCallback mLoginCallback;
    //获取到第三方信息时回调
    private OnLoginSuccessListener mOnLoginSuccessListener;

    public ThirdLoginPresenter(Context context){
        this.mContext = context;
    }

    public ThirdLoginPresenter(Context context,LoginCallback loginCallback){
        this.mContext = context;
        this.mLoginCallback = loginCallback;
    }

    public ThirdLoginPresenter(Context context,LoginCallback loginCallback,OnLoginSuccessListener listener){
        this.mContext = context;
        this.mLoginCallback = loginCallback;
        this.mOnLoginSuccessListener = listener;
    }

    /****回调监听****/
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int action, Map<String, String> data) {
            //完成
            getDataByAuthType(share_media, data);
            if (mLoginCallback != null) {
                mLoginCallback.loginLoading();
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int action, Throwable t) {
            ToastUtil.getInstance().show("授权出错啦");
            if (mLoginCallback != null) {
                mLoginCallback.loginCancel(share_media);
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int action) {
            ToastUtil.getInstance().show("授权取消");
            if (mLoginCallback != null) {
                mLoginCallback.loginCancel(share_media);
            }
        }
    };


    /***获取第三方的用户数据****/
    private void getDataByAuthType(SHARE_MEDIA share_media, Map<String, String> data) {
        String dataJson = new Gson().toJson(data);
        switch (share_media) {
            case QQ:
                QQUserInfo userInfoQQ = new Gson().fromJson(dataJson, QQUserInfo.class);

                Logger.d("QQ"+dataJson);

                if (mOnLoginSuccessListener != null) {
                    mOnLoginSuccessListener.onTencentQQ(userInfoQQ);

                } else {
//                    凉瓜应用登录
//                    thirdLogin(0, userInfoQQ.access_token, userInfoQQ.openid);
                }

                break;
            case WEIXIN:
                WXUserInfo userInfoWX = new Gson().fromJson(dataJson, WXUserInfo.class);

                if (mOnLoginSuccessListener != null) {
                    mOnLoginSuccessListener.onWeChat(userInfoWX);

                } else {
//                    凉瓜应用登录
//                    thirdLogin(1, userInfoWX.accessToken, userInfoWX.openid);
                }

                break;
            case SINA:
                WBUserInfo userInfoWB = new Gson().fromJson(dataJson, WBUserInfo.class);
                if (mOnLoginSuccessListener!=null){
                    mOnLoginSuccessListener.onSINA(userInfoWB);
                }else {
//                    凉瓜应用登录
//                    thirdLogin(2, userInfoWB.access_token, userInfoWB.uid);
                }
                break;
        }

    }


    public void setOnLoginSuccessListener(OnLoginSuccessListener onLoginSuccessListener) {
        this.mOnLoginSuccessListener = onLoginSuccessListener;
    }

    /**
     * 记得调用这个方法
     * QQ和新浪微博的登录回调
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(mContext).onActivityResult(requestCode, resultCode, data);
    }

    public interface BindThirdPlatformListener {
        void onBindWeChat(WXUserInfo userInfo);
    }

    public interface OnLoginSuccessListener {
        void onTencentQQ(QQUserInfo userInfo);

        void onWeChat(WXUserInfo userInfo);

        void onSINA(WBUserInfo userInfo);
    }

    /******微信登录******/
    public void loginWX() {
        if (!UMShareAPI.get(mContext).isInstall((Activity) mContext, SHARE_MEDIA.WEIXIN)) {
            ToastUtil.getInstance().show("请先安装微信!");
            if (mLoginCallback != null) {
                mLoginCallback.loginError(SHARE_MEDIA.WEIXIN);
            }
            return;
        }
        UMShareAPI.get(mContext).getPlatformInfo((Activity) mContext, SHARE_MEDIA.WEIXIN, umAuthListener);
    }


    /******QQ登录******/
    public void loginQQ() {
        if (!UMShareAPI.get(mContext).isInstall((Activity) mContext, SHARE_MEDIA.QQ)) {
            ToastUtil.getInstance().show("请先安装QQ!");
            if (mLoginCallback != null) {
                mLoginCallback.loginError(SHARE_MEDIA.QQ);
            }
            return;
        }
        UMShareAPI.get(mContext).getPlatformInfo((Activity) mContext, SHARE_MEDIA.QQ, umAuthListener);
    }

    /******微博登录******/
    public void loginWB() {
        if (!UMShareAPI.get(mContext).isInstall((Activity) mContext, SHARE_MEDIA.SINA)) {
            ToastUtil.getInstance().show("请先安装微博!");
            if (mLoginCallback != null) {
                mLoginCallback.loginError(SHARE_MEDIA.SINA);
            }
            return;
        }
        UMShareAPI.get(mContext).getPlatformInfo((Activity) mContext, SHARE_MEDIA.SINA, umAuthListener);
    }

}
